﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyVoteMVC.ExcelObject;
using MyVoteMVC.BackstageManagement.Controllers;
using System.IO;

namespace MyVoteMVC.BackstageManagement.Views
{

    public partial class ProjectsImportor : Form, IProjectsImportorView
    {
        private IProjectsImportorController controller;
        public ProjectsImportor()
        {
            InitializeComponent();

            controller = new ProjectsImportorController(this);
        }


        public IController<Project> MyController
        {
            get
            {
                return controller;
            }
        }

        
        public void LoadExcelFileComplete(IEnumerable<string> sheetNames)
        {
            this.lvSheet.Items.Clear();
            foreach (string sheetName in sheetNames)
            {
                this.lvSheet.Items.Add(new ListViewItem(sheetName));
            }
        }

        public void ExcelQueryComplete(IEnumerable<Project> projects)
        {
            gvProjects.DataSource = projects;
            EnableControl(true);
        }

        public void DBSubmitComplete(string message)
        {
            MessageBox.Show(message);
            EnableControl(true);
        }

        public void LoadComplete(Project loadedEntity)
        {
            throw new NotImplementedException();
        }
        
        private void ProjectsImportor_Load(object sender, EventArgs e)
        {
           // controller.Load();
        }

        private void btnSetImg_Click(object sender, EventArgs e)
        {
            if (imageFolderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string folderPath = imageFolderBrowserDialog.SelectedPath;
                txtImageFolder.Text = folderPath;
            }
        }

        private void btnSetExcel_Click(object sender, EventArgs e)
        {
            //文本文件(*.txt)|*.txt|所有文件(*.*)|*.*
            excelOpenFileDialog.Filter = "Excel2003文件(*.xls)|*.xls|Excel2007文件(*.xlsx)|*.xlsx|CSV文件(*.csv)|*.csv";
            if (excelOpenFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string filePath = excelOpenFileDialog.FileName;
                txtExcel.Text = filePath;
                controller.LoadExcelFile(filePath);
            }
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
         
            if (string.IsNullOrWhiteSpace(txtAddress.Text)
                || string.IsNullOrWhiteSpace(txtName.Text)
                || string.IsNullOrWhiteSpace(txtType.Text)
                || string.IsNullOrWhiteSpace(txtYear.Text)
                 || string.IsNullOrWhiteSpace(txtAwards.Text)
                   || string.IsNullOrWhiteSpace(txtDescription.Text)
                )
            {
                MessageBox.Show("请检测项目名、项目类型、项目地址、项目年份、项目获奖情况的映射是否填好");
                return;
            }

            if (string.IsNullOrWhiteSpace(txtExcel.Text)
                || string.IsNullOrWhiteSpace(txtImageFolder.Text)
                || !File.Exists(txtExcel.Text)
                || !Directory.Exists(txtImageFolder.Text))
            {
                MessageBox.Show("请检测Excel文件和图片路径是否设置好");
                return;
            }

            List<string> sheetsToConvert = new List<string>();
            if (lvSheet.SelectedItems == null || lvSheet.SelectedItems.Count == 0)
            {
                MessageBox.Show("请选中要进行转换的Excel表");
                return;
            }
            else
            {
                foreach (ListViewItem item in lvSheet.SelectedItems)
                {
                    sheetsToConvert.Add(item.Text);
                }
            }

            EnableControl(false);

            controller.ExcelQuery(txtName.Text,
                txtType.Text, 
                txtYear.Text, 
                txtAddress.Text, 
                txtAwards.Text,
                txtDescription.Text,
                txtImageFolder.Text,
                sheetsToConvert
                );
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (gvProjects.DataSource != null)
            {
                EnableControl(false);
                controller.DBSubmit(gvProjects.DataSource as IList<Project>);
            }
        }

        void EnableControl(bool enable)
        {
            btnConvert.Enabled = enable;
            btnSetExcel.Enabled = enable;
            btnSetImg.Enabled = enable;
            btnSubmit.Enabled = enable;

            txtAddress.Enabled = enable;
            txtName.Enabled = enable;
            txtType.Enabled = enable;
            txtYear.Enabled = enable;
            txtAwards.Enabled = enable;
            txtDescription.Enabled = enable;

            txtExcel.Enabled = enable;
            txtImageFolder.Enabled = enable;
        }

    }
}
